package com.mfnote.view.record;

import java.io.File;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mfnote.adapter.RecordAdapter;
import com.mfnote.db.dao.HandDao;
import com.mfnote.db.dao.RecordDao;
import com.mfnote.util.FinalData;
import com.mfnote.view.R;
import com.mfnote.view.R.anim;
import com.mfnote.view.R.id;
import com.mfnote.view.R.layout;
import com.mfnote.view.main.MainActivity;
import com.mfnote.view.other.MyApplication;

/**
 * 录音列表界面
 * @author DC
 *
 */
public class RecordActivity extends Activity implements OnClickListener,
		OnItemLongClickListener, OnItemClickListener {
	//新建录音按钮
	private Button record_new_bt = null;
    //新建录音TextView
	private TextView record_new_record_tv = null;
    //录音列表
	private ListView record_items_lv = null;
	//ListView上面的细线
	private TextView first_empty_line_tv =null;
    //当前点击的Item的数据
	private HashMap<String, Object> map = null;
    //录音文件的数据库处理
	private RecordDao recordDao = null;
	private AlertDialog alertDlgIsDel; //长按记录弹出删除提示框
	private LinearLayout ll_first;
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
//			if(recordDao.getRecordAllCount().size()>0){
//				first_empty_line_tv.setVisibility(View.VISIBLE);
//			}else{
//				first_empty_line_tv.setVisibility(View.GONE);
//			}
			switch (msg.what) {
			case 20:
				record_items_lv.setAdapter(new RecordAdapter(getApplicationContext()));
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.record);
		MyApplication.getInstance().addActivity(this);

		recordDao = new RecordDao(this);

		ll_first=(LinearLayout) findViewById(R.id.record_ll_first);
		record_new_bt = (Button) this.findViewById(R.id.record_new_bt);
		//record_new_record_tv = (TextView) this.findViewById(R.id.record_new_record_tv);
		//first_empty_line_tv = (TextView) this.findViewById(R.id.first_empty_line_tv);
		record_items_lv = (ListView) this.findViewById(R.id.record_items_lv);

		ll_first.setOnClickListener(this);
		record_new_bt.setOnClickListener(this);
		//record_new_record_tv.setOnClickListener(this);
		record_items_lv.setAdapter(new RecordAdapter(this));
		record_items_lv.setOnItemClickListener(this);
		record_items_lv.setOnItemLongClickListener(this);

//		if(recordDao.getRecordAllCount().size()>0){
//			first_empty_line_tv.setVisibility(View.VISIBLE);
//		}else{
//			first_empty_line_tv.setVisibility(View.GONE);
//		}
	}
    
	/**button的监听**/
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.record_new_bt:
			Intent intent_1 = new Intent(this, NewRecordActivity.class);
			startActivity(intent_1);
			finish();
			overridePendingTransition(R.anim.push_right_in,
					R.anim.push_left_out);
			break;
		case R.id.record_ll_first:
			Intent intent_2 = new Intent(this, NewRecordActivity.class);
			startActivity(intent_2);
			finish();
			overridePendingTransition(R.anim.push_right_in,
					R.anim.push_left_out);
			break;

		}
	}

	/**ListView的单击监听**/
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(this, NewRecordActivity.class);
		map = (HashMap<String, Object>) parent.getItemAtPosition(position);
		if ((Integer) (map.get("recordLength")) < 1) {
			Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show();
			// 删除该条目
			long state = recordDao.deleteRecord(map);
			if (state > 0) {
				Toast.makeText(this, "该条目已移除", Toast.LENGTH_SHORT).show();
				// 通知主线程更新ListView
				Message msg = new Message();
				msg.what = 20;
				handler.sendMessage(msg);
			}else{
				Toast.makeText(this, "该条目移除出错", Toast.LENGTH_SHORT).show();
			}
		}

		intent.putExtra("map", map);
		startActivity(intent);
		finish();
		overridePendingTransition(R.anim.push_right_in,
				R.anim.push_left_out);
	}
    
	/**ListView的长按监听**/
	@Override
	public boolean onItemLongClick(final AdapterView<?> parent, View view,
			final int position, long id) {
		LayoutInflater inflater=LayoutInflater.from(this);
		View viewDialog=inflater.inflate(R.layout.dialog_isdelete, null);
		alertDlgIsDel= new AlertDialog.Builder(this).create();
		alertDlgIsDel.show();
		Display display=this.getWindowManager().getDefaultDisplay();
		int width=display.getWidth();
		int height=display.getHeight();
		LayoutParams layoutParams=new LayoutParams(width*90/100,LayoutParams.WRAP_CONTENT);	
		alertDlgIsDel.setContentView(viewDialog, layoutParams);
		Button btn_delete_sure=(Button) viewDialog.findViewById(R.id.btn_delete_sure);
		Button btn_delete_cancel=(Button) viewDialog.findViewById(R.id.btn_delete_cancel);
		btn_delete_sure.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//删除数据
				HashMap<String, Object> hashMap = (HashMap<String, Object>) parent
						.getItemAtPosition(position);
				long state = recordDao.deleteRecord(hashMap);
				if (state > 0) {
					Toast.makeText(RecordActivity.this, "删除成功",Toast.LENGTH_SHORT).show();
					// 通知主线程更新ListView
					Message msg = new Message();
					msg.what = 20;
					handler.sendMessage(msg);
					// 删除文件
					File file = new File(hashMap.get("recordPath").toString());
					file.delete();
				} else {
					Toast.makeText(RecordActivity.this, "删除失败",Toast.LENGTH_SHORT).show();
				}
				alertDlgIsDel.dismiss();
			}
		});
		btn_delete_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				alertDlgIsDel.dismiss();
			}
		});
//		AlertDialog.Builder builder = new AlertDialog.Builder(this);
//		builder.setTitle("提示");
//		builder.setMessage("删除该录音");
//		builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
//			@Override
//			public void onClick(DialogInterface dialog, int which) {
//				HashMap<String, Object> hashMap = (HashMap<String, Object>) parent
//						.getItemAtPosition(position);
//				long state = recordDao.deleteRecord(hashMap);
//				if (state > 0) {
//					Toast.makeText(RecordActivity.this, "删除成功",Toast.LENGTH_SHORT).show();
//					// 通知主线程更新ListView
//					Message msg = new Message();
//					msg.what = 20;
//					handler.sendMessage(msg);
//					// 删除文件
//					File file = new File(hashMap.get("recordPath").toString());
//					file.delete();
//				} else {
//					Toast.makeText(RecordActivity.this, "删除失败",Toast.LENGTH_SHORT).show();
//				}
//
//			}
//		});
//
//		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//			@Override
//			public void onClick(DialogInterface dialog, int which) {
//
//			}
//
//		});
//
//		builder.create().show();
		return true;
	}
    
	/**监听返回键的处理**/
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			finish();
			overridePendingTransition(R.anim.alpha_in,R.anim.scale_suoxiao_out
					);
		}
		return true;
	}

}
